﻿using System.ComponentModel;
using Microsoft.VisualStudio.TestTools.WebTesting;
using QA.TFS.Utils;

namespace QA.TFS.Plugins.Request
{
    public class DisableRequest : WebTestRequestPlugin
    {
        private bool _IsDisabled = false;

        [DefaultValue(false)]
        public bool IsDisabled
        {
            get { return _IsDisabled; }
            set { _IsDisabled = value; }
        }

        private string _ReplaceLocation = string.Empty;

        public string ReplaceLocation
        {
            get { return _ReplaceLocation; }
            set { _ReplaceLocation = value; }
        }

        public override void PreRequest(object sender, PreRequestEventArgs e)
        {
            if (IsDisabled)
            {
                if (string.IsNullOrEmpty(ReplaceLocation))
                    e.Request.Url = e.WebTest.Context["URLServer"].ToString();
                else
                {
                    ReplaceLocation = ContextParameterHelper.replaceContextParameters(e.WebTest.Context, ReplaceLocation);
                    e.Request.Url = ReplaceLocation;
                }

                e.Request.ParseDependentRequests = false;
                e.Request.RecordResult = false;
                e.Request.FollowRedirects = false;
                e.Request.ExpectedHttpStatusCode = 405;
            }
            else
                base.PreRequest(sender, e);
        }
    }

}
